﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace ContaTelefone.Data
{
    public class CONTA_LIGACAORepository
    {
        ContaTelefoneEntities entity;
        SqlConnection cn;
        SqlCommand cmd;

        public CONTA_LIGACAORepository()
        {
            entity = ContaTelefoneContext.getInstance();
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ContaTelefoneADODb"].ConnectionString);
            cn.Open();
            cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = @"INSERT INTO CONTA_LIGACAO(ID_CONTA,ID,NumNF,NumIDCli,MesRef,NumAcs,Plano,Nome,Tpserv,Data,Origem,Destino,NumCham,Tipo,Duracao,Valor,OperLD,TIMID,Alíquota,CodFEBRABAN,TotalGeral)
                               VALUES(@ID_CONTA,@ID,@NumNF,@NumIDCli,@MesRef,@NumAcs,@Plano,@Nome,@Tpserv,@Data,@Origem,@Destino,@NumCham,@Tipo,@Duracao,@Valor,@OperLD,@TIMID,@Alíquota,@CodFEBRABAN,@TotalGeral)";
            
            cmd.Parameters.Add("@ID_CONTA", System.Data.SqlDbType.Int);
            cmd.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
            cmd.Parameters.Add("@NumNF", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@NumIDCli", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@MesRef", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@NumAcs", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Plano", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Nome", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Tpserv", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Data", System.Data.SqlDbType.DateTime);
            cmd.Parameters.Add("@Origem", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Destino", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@NumCham", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Tipo", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Duracao", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Valor", System.Data.SqlDbType.Decimal);
            cmd.Parameters.Add("@OperLD", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@TIMID", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Alíquota", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@CodFEBRABAN", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@TotalGeral", System.Data.SqlDbType.Decimal);

        }

        public void RefreshEntity()
        {
            entity.Dispose();
            ContaTelefoneContext.removeInstance();
            entity = ContaTelefoneContext.getInstance();
        }

        public IQueryable<CONTA_LIGACAO> GetCONTA_LIGACAOs()
        {
            return ContaTelefoneContext.getInstance().CONTA_LIGACAO;
        }

        public IList<CONTA_LIGACAO> GetAll()
        {
            return ContaTelefoneContext.getInstance().CONTA_LIGACAO.ToList();
        }

        public CONTA_LIGACAO getCONTA_LIGACAO(int pID_CONTA, long pID)
        {
            return entity.CONTA_LIGACAO.SingleOrDefault(a => a.ID_CONTA == pID_CONTA && a.ID == pID);
        }

        public void Update()
        {
            try
            {
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                ContaTelefoneContext.removeInstance();
                throw ex;
            }
        }

        public void Add(CONTA_LIGACAO pCONTA_LIGACAO)
        {
            try
            {
                entity.AddToCONTA_LIGACAO(pCONTA_LIGACAO);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                ContaTelefoneContext.removeInstance();
                throw ex;
            }
        }

        public void AddADODb(CONTA_LIGACAO pCONTA_LIGACAO)
        {
            try
            {
                cmd.Parameters["@ID_CONTA"].Value = pCONTA_LIGACAO.ID_CONTA;
                cmd.Parameters["@ID"].Value = pCONTA_LIGACAO.ID;
                cmd.Parameters["@NumNF"].Value = pCONTA_LIGACAO.NumNF;
                cmd.Parameters["@NumIDCli"].Value = pCONTA_LIGACAO.NumIDCli;
                cmd.Parameters["@MesRef"].Value = pCONTA_LIGACAO.MesRef;
                cmd.Parameters["@NumAcs"].Value = pCONTA_LIGACAO.NumAcs;
                cmd.Parameters["@Plano"].Value = pCONTA_LIGACAO.Plano;
                cmd.Parameters["@Nome"].Value = pCONTA_LIGACAO.Nome;
                cmd.Parameters["@Tpserv"].Value = pCONTA_LIGACAO.Tpserv;
                cmd.Parameters["@Data"].Value = pCONTA_LIGACAO.Data;
                cmd.Parameters["@Origem"].Value = pCONTA_LIGACAO.Origem;
                cmd.Parameters["@Destino"].Value = pCONTA_LIGACAO.Destino;
                cmd.Parameters["@NumCham"].Value = pCONTA_LIGACAO.NumCham;
                cmd.Parameters["@Tipo"].Value = pCONTA_LIGACAO.Tipo;
                cmd.Parameters["@Duracao"].Value = pCONTA_LIGACAO.Duracao;
                cmd.Parameters["@Valor"].Value = pCONTA_LIGACAO.Valor;
                cmd.Parameters["@OperLD"].Value = pCONTA_LIGACAO.OperLD;
                cmd.Parameters["@TIMID"].Value = pCONTA_LIGACAO.TIMID;
                cmd.Parameters["@Alíquota"].Value = pCONTA_LIGACAO.Alíquota;
                cmd.Parameters["@CodFEBRABAN"].Value = pCONTA_LIGACAO.CodFEBRABAN;
                cmd.Parameters["@TotalGeral"].Value = pCONTA_LIGACAO.TotalGeral;

                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ContaTelefoneContext.removeInstance();
                throw ex;
            }
        }

        public void AddSemSalvar(CONTA_LIGACAO pCONTA_LIGACAO)
        {
            try
            {
                entity.AddToCONTA_LIGACAO(pCONTA_LIGACAO);
            }
            catch (Exception ex)
            {
                ContaTelefoneContext.removeInstance();
                throw ex;
            }
        }

        public void Delete(CONTA_LIGACAO pCONTA_LIGACAO)
        {
            entity.CONTA_LIGACAO.DeleteObject(pCONTA_LIGACAO);
            entity.SaveChanges();
        }
    }
}
